package com.central.apps.handler.contract;

import com.alibaba.fastjson.JSONObject;
import com.central.app.common.util.ExcelUtil;
import com.central.app.service.HandlerService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2022/9/16.
 */
@RestController
@Api(tags = "项目计划接口类")
@RequestMapping("/contract")
@Slf4j
public class TextimportController {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/import")
    public String importContract(HttpServletRequest request, Integer projectId) throws Exception {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        JSONObject json = new JSONObject();
        MultipartFile file = null;
        if (files.size()>0){
            file = files.get(0);
        }else {
            json.put("msg", "文件为空");
            return json.toString();
        }
        HashMap<String, ArrayList<String[]>> hashMap = ExcelUtil.analysisFile(file);
        ArrayList<String[]> arrayList = hashMap.get("OK");
        if(CollectionUtils.isEmpty(arrayList)){
            Set<String> strings = hashMap.keySet();
            String next = strings.iterator().next();
            json.put("msg", next);
            return json.toString();
        }

        Map<String, Object> map = new HashMap<>();
        if(!CollectionUtils.isEmpty(arrayList)){
            map.put("conname",arrayList.get(0)[0]);
            map.put("contype",arrayList.get(0)[1]);
            map.put("relationprocode",arrayList.get(0)[2]);
            map.put("relationproname",arrayList.get(0)[3]);
            map.put("maincon",arrayList.get(0)[4]);
            map.put("relationname",arrayList.get(0)[5]);
            map.put("orgdesc",arrayList.get(0)[6]);
            map.put("sitedesc",arrayList.get(0)[7]);
            map.put("deptdesc",arrayList.get(0)[8]);
            map.put("starttime",arrayList.get(0)[9]);
            map.put("endtime",arrayList.get(0)[10]);
            map.put("totalprice",arrayList.get(0)[11]);
            map.put("oppcompany",arrayList.get(0)[12]);
            LoginAppUser user = SysUserUtil.getLoginAppUser();
            //获取部门编码
            String deptcode =user.getDeptcode();
            map.put("deptcode",deptcode);
            map.put("createuserdesc",user.getCreateuserdesc());
            handlerService.insert("gq_consign", map);
        }

        json.put("code", "0");
        json.put("msg", "导入成功");
        return json.toString();
    }
}
